package com.yxhl.zoume.common.ui.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.yxhl.zoume.utils.DensityUtils;

/**
 * Created by vhawk on 15/9/25.
 * 自适应item数量的listview
 * <p>
 * modify by sugarya on 16/05/15
 */
public class ListViewWrap extends ListView {

    public static final int ITEM_HEIGHT = 42;
    public static final int MARGIN_VALUE = 10;
    private static final String TAG = "ListViewWrap";


    public ListViewWrap(Context context) {
        super(context);
    }

    public ListViewWrap(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewWrap(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ListAdapter listAdapter = this.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int childCount = getCount();
        int totalHeight = childCount * DensityUtils.dip2px(getContext(), ITEM_HEIGHT);

        ViewGroup.LayoutParams params = this.getLayoutParams();
        ((MarginLayoutParams) params).setMargins(0, MARGIN_VALUE, 0, 0);

        int needHeight = totalHeight + (this.getDividerHeight() * (getCount() - 1));
        int needHeightMeasureSpec = MeasureSpec.makeMeasureSpec(needHeight, MeasureSpec.AT_MOST);

        setMeasuredDimension(widthMeasureSpec, needHeightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                return false;
        }
        return super.onTouchEvent(ev);
    }


}
